# Copyright 2020 by Kurt Rathjen. All Rights Reserved.
#
# This library is free software: you can redistribute it and/or modify it 
# under the terms of the GNU Lesser General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version. This library is distributed in the 
# hope that it will be useful, but WITHOUT ANY WARRANTY; without even the 
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# See the GNU Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.

import os
import shutil
import logging

from FXstudiovendor.Qt import QtWidgets

import FXmutils
import FXmutils.gui

try:
	import maya.cmds
	import maya.cmds as cmds
	import maya.mel as mel
except ImportError:
	import traceback
	traceback.print_exc()



def Istype(int , path):
	for root, dirs, files in os.walk(path):
		if"sequence" in root and not ".mayaSwatches" in root:
			for file_name in os.listdir(root):
				file_path = os.path.join(root, file_name)
				if os.path.isfile(file_path):
					pass
	selectObject = cmds.ls(sl=1)
	if selectObject:
		Object_T = cmds.xform(selectObject[0] , q=1,translation=1, worldSpace=1)
		wave_Cv = Create_FX(int,file_path.replace("\\","/"))
		cmds.xform(wave_Cv,r = 1,translation=Object_T)
	else:
		Create_FX(int,file_path.replace("\\","/"))

def Create_FX(int , path):
	Path = os.path.dirname(path).split("/")[-1]
	Pic_Name = os.path.dirname(path).split("/")[-1]
	ImgFilePath = path
	PicPolyLens = []
	List_AllTransform = cmds.ls(transforms=1)
	for List_PolyS in List_AllTransform:
		if Pic_Name in List_PolyS:
			for Polys in cmds.listHistory(List_PolyS):
				if 'polyPlane' in Polys or 'polySphere' in Polys or 'polyCylinder' in Polys or 'polyCone' in Polys  or 'pTorusShape' in Polys or 'pTorus' in Polys:
					PicPolyLens.append(Polys)
	print(PicPolyLens)
	Pic_version = ''
	for i in range(len(PicPolyLens)):
		if i + 1 == len(PicPolyLens):
			Pic_version = str(i + 1) 

	
	if int == 1 :
		main_A = cmds.circle(n=Pic_Name + 'main_A' + Pic_version, nr=[0, 1, 0], sw=360, d=3, r=1.5, ut=0, ch=0, tol=0.01)[0]
		main_B = cmds.circle(n=Pic_Name + 'main_B' + Pic_version, nr=[0, 1, 0], sw=360, d=3, r=1, ut=0, ch=0, tol=0.01)[0]
		polyP_Grp = cmds.group(em=True, name=Pic_Name + '_Grp' + Pic_version)

		polyP = cmds.polyPlane ( n =Pic_Name + Pic_version,w = 1,h = 1 ,sx=10 ,sy = 10 ,ax=[0, 1 ,0] ,cuv = 2 ,ch=1)
		polyP_Transform = polyP[0]

		nonLinear = cmds.nonLinear ( polyP_Transform,type ="wave")
		sine = cmds.rename ( nonLinear[0],Pic_Name + Pic_version +"_wave")
		Handle = cmds.rename ( nonLinear[1],Pic_Name + Pic_version +"_waveHandle")


		
	elif int ==2 :
		
		main_A = cmds.circle(n=Pic_Name + 'main_A' + Pic_version,c=[ 0 ,0 ,0],nr =[0 ,1,0],radius=15)[0]
		main_B = cmds.circle(n=Pic_Name + 'main_B' + Pic_version,c=[ 0 ,0 ,0],nr =[0 ,1,0],radius=13)[0]
		polyP_Grp = cmds.group(em=True, name=Pic_Name + '_Grp' + Pic_version)

		polyP = cmds.polyCylinder (n=Pic_Name + Pic_version,r =1,h =5 ,sx= 20 ,sy= 1 ,sz=1 ,ax= (0 ,0 ,1),rcp= 0 ,cuv= 3 ,ch =1)
		polyP_Transform = polyP[0]
		nonLinear = cmds.curve(n=Pic_Name + Pic_version + "_Cv",p =[(0,0,12),(0,0,10.666667),(0,0,8),(0,0,4),(0,0,0),(0,0,-4),(0,0,-8),(0,0,-10.666667),(0,0,-12)] ,k=[0,0,0,1,2,3,4,5,6,6,6])
		
		curveshapes = cmds.listRelatives(nonLinear, shapes=True)

		cmds.setAttr (curveshapes[0] + ".overrideEnabled", 1)
		cmds.setAttr (curveshapes[0] + ".overrideDisplayType", 2)

		point_list = cmds.ls(nonLinear + '.cv[*]', flatten=True)
		Cv_Grp =cmds.createNode( "transform", n ="Cv_Grp" + Pic_version) 
		Handle = Cv_Grp
		clu_Grp =cmds.createNode( "transform", n ="clu_Grp" + Pic_version) 
		for point in range(len(point_list)):
			point_Name =  point_list[point]
			position = cmds.xform(point_Name, query=True, translation=True, worldSpace=True)
			clu = cmds.cluster(  point_Name,rel=True ,name=nonLinear)
			wave_Cv =cmds.circle (n = nonLinear + "_Cv" + str(point) ,c=[ 0 ,0 ,0],nr =[0 ,0,1] )[0]
			cmds.xform(wave_Cv, r= 1,translation=position)
			cmds.parentConstraint (wave_Cv, clu[1],mo =1,weight=1 )
			cmds.scaleConstraint(wave_Cv, clu[1],mo =1,weight=1 )
			cmds.setAttr (clu_Grp+".hiddenInOutliner", 1)
			cmds.setAttr (clu_Grp+".visibility", 0)
			
			cmds.parent (wave_Cv, Cv_Grp )  
			cmds.parent (clu, clu_Grp )

		cmds.parent (nonLinear, polyP_Grp ) 

		modshapes = cmds.listRelatives(polyP_Transform, shapes=True)
		Deformer = cmds.deformer (modshapes,type ="curveWarp" ,n = polyP_Transform + "curveWarp")
		cmds.connectAttr(curveshapes[0] + ".worldSpace[0]" ,Deformer[0]+".inputCurve")

	elif int ==3 :
		main_A = cmds.circle ( n = Pic_Name + 'main_A' + Pic_version, nr=[0, 1, 0], sw=360, d=3, r=1.5, ut=0, ch=0, tol=0.01)[0]
		main_B = cmds.circle ( n = Pic_Name + 'main_B' + Pic_version, nr=[0, 1, 0], sw=360, d=3, r=1, ut=0, ch=0, tol=0.01)[0]
		polyP_Grp = cmds.group ( em = True, name=Pic_Name + '_Grp' + Pic_version)

		polyP = cmds.polyPlane ( n=Pic_Name + Pic_version,w =1,h =1 ,sx= 10 ,sy =10,ax=( 1 ,0 ,0),cuv =2,ch =1)
		polyP_Transform = polyP[0]

		cmds.xform ( polyP_Transform,r=True,rotatePivot=(0, -0.5, 0) )
		cmds.xform ( polyP_Transform,r=True,translation=(0, 0.5, 0) )
		cmds.makeIdentity ( polyP_Transform,apply= 1 ,t= 1 ,r= 0,s =0 ,n =0,pn =1)

		nonLinear = cmds.nonLinear (polyP_Transform,type ="sine" )
		sine = cmds.rename(nonLinear[0],Pic_Name + Pic_version +"_sine")
		Handle = cmds.rename(nonLinear[1],Pic_Name + Pic_version +"_sineHandle")

		cmds.setAttr (Handle + ".translateY" ,0)
		cmds.setAttr (Handle + ".scaleZ" ,1)
		cmds.setAttr (Handle + ".scaleX", 1)
		cmds.setAttr (Handle + ".scaleY" ,1)
		cmds.setAttr (sine+ ".lowBound" ,0)

		cmds.setAttr (Handle + ".overrideEnabled", 1)
		cmds.setAttr (Handle + ".overrideDisplayType", 2)
		cmds.setAttr (Handle + ".hiddenInOutliner",1)
		

	#cmds.parentConstraint(main_B, polyP_Transform)
	#cmds.scaleConstraint(main_B, polyP_Transform, mo=1, weight=1)
	
	cmds.parent ( Handle , polyP_Transform , main_B ) 
	cmds.parent ( main_B , main_A )
	cmds.parent ( main_A , polyP_Grp )

	lambertName = cmds.shadingNode('lambert', asShader=True, n=polyP_Transform + '_labt')
	lambertSG = cmds.sets(renderable=1, noSurfaceShader=1, empty=1, name=polyP_Transform + '_SG')
	cmds.connectAttr(lambertName + '.outColor', lambertSG + '.surfaceShader', f=1)
	cmds.sets(polyP_Transform, e=1, forceElement=lambertSG)
	shadingNodefile = cmds.shadingNode('file', asTexture=1, isColorManaged=1, n=polyP_Transform + '_file')

	cmds.setAttr(shadingNodefile + '.fileTextureName', ImgFilePath, type="string")
	cmds.setAttr(shadingNodefile + '.useFrameExtension', 1)

	cmds.connectAttr(shadingNodefile + '.outColor', lambertName + '.color', f=1)
	TextureName = cmds.shadingNode('place2dTexture', asUtility=1, n=polyP_Transform + '_Texture')
	Nodes = ['coverage', 'translateFrame', 'rotateFrame', 'mirrorU', 'mirrorV', 'stagger', 'wrapU',
			 'wrapV', 'repeatUV', 'offset', 'rotateUV', 'noiseUV', 'vertexUvOne', 'vertexUvTwo',
			 'vertexUvThree', 'vertexCameraOne']
	for Node in Nodes:
		cmds.connectAttr(TextureName + '.' + Node, shadingNodefile + '.' + Node, f=1)
	cmds.connectAttr(TextureName + '.outUV', shadingNodefile + '.uv')
	cmds.connectAttr(TextureName + '.outUvFilterSize', shadingNodefile + '.uvFilterSize')

	cmds.defaultNavigation(force=1, connectToExisting=1, source=shadingNodefile, destination=lambertName + '.color')
	cmds.connectAttr(shadingNodefile + '.outTransparency', lambertName + '.transparency', f=1)
	cmds.select(shadingNodefile, r=1)


	cmds.addAttr ( main_A , ln = 'ShiJianPanYi', at='long', dv=0)
	cmds.addAttr ( main_A , ln = 'BaoGuang', at='float', min=-5, max=5, dv=0)
	cmds.addAttr ( main_A , ln = 'TouMingTongDao', at='float', min=0, max=2, dv=1)
	cmds.addAttr ( main_A , ln = 'ColorGainR', at='float', min=0, max=20, dv=0)
	cmds.addAttr ( main_A , ln = 'ColorGainG', at='float', min=0, max=20, dv=0)
	cmds.addAttr ( main_A , ln = 'ColorGainB', at='float', min=0, max=20, dv=0)
	cmds.addAttr ( main_A , ln = "XingBian",at ="enum",en = "--------------------------:")
	cmds.addAttr ( main_A , ln = 'RuDong', at='float', dv=0)
	cmds.addAttr ( main_A , ln = 'NoiseU', at='float', min=0, dv=0)
	cmds.addAttr ( main_A , ln = 'NoiseV', at='float', min=0, dv=0)

	cmds.setAttr ( main_A + '.ShiJianPanYi', e=1, keyable=1)
	cmds.setAttr ( main_A + '.BaoGuang', e=1, keyable=1)
	cmds.setAttr ( main_A + '.TouMingTongDao', e=1, keyable=1)
	cmds.setAttr ( main_A + '.ColorGainR', e=1, keyable=1)
	cmds.setAttr ( main_A + '.ColorGainG', e=1, keyable=1)
	cmds.setAttr ( main_A + '.ColorGainB', e=1, keyable=1)
	cmds.setAttr ( main_A + ".XingBian" , e = 1 , keyable = 1 )
	cmds.setAttr ( main_A + ".RuDong" , e = 1 , keyable = 1 )
	cmds.setAttr ( main_A + ".NoiseU" , e = 1 , keyable = 1 )
	cmds.setAttr ( main_A + ".NoiseV" , e = 1 , keyable = 1 )

	cmds.connectAttr(main_A + '.ShiJianPanYi', shadingNodefile + '.io' , f=1 )
	cmds.connectAttr(main_A + '.BaoGuang', shadingNodefile + '.exp' , f=1 )
	cmds.connectAttr(main_A + '.TouMingTongDao', shadingNodefile + '.ag', f=1 )
	cmds.connectAttr(main_A + '.ColorGainR', shadingNodefile + '.cor' , f=1 )
	cmds.connectAttr(main_A + '.ColorGainG', shadingNodefile + '.cog' , f=1 )
	cmds.connectAttr(main_A + '.ColorGainB', shadingNodefile + '.cob' , f=1 )
	cmds.connectAttr(main_A + '.RuDong', TextureName + '.rotateUV' , f=1 )
	cmds.connectAttr(main_A + '.NoiseU', TextureName + '.noiseU' , f=1 )
	cmds.connectAttr(main_A + '.NoiseV', TextureName + '.noiseV' , f=1 )

	if int == 1 :
		

		cmds.addAttr(main_A,ln ="wave",at ="enum",en = "--------------------------:")
		cmds.addAttr(main_A,ln ="offset",at ="double",min = -10 , max = 10,dv=0)
		cmds.addAttr(main_A,ln ="envelope",at ="double",min = 0 , max = 1,dv=1)
		cmds.addAttr(main_A,ln ="amplitude",at ="double",min = -5 , max = 5,dv=0.1)
		cmds.addAttr(main_A,ln ="wavelength",at ="double",min = 0.1 , max = 10,dv=1)
		cmds.addAttr(main_A,ln ="dropoff",at ="double",min = -1 , max = 1,dv=0)
		cmds.addAttr(main_A,ln ="dropoffPosition",at ="double",min = 0 , max = 1,dv=0)
		cmds.addAttr(main_A,ln ="minRadius",at ="double",min = 0 , max = 10,dv=0)
		cmds.addAttr(main_A,ln ="maxRadius",at ="double",min = 0 , max = 10,dv=1)

		cmds.setAttr (main_A + ".wave",e =1,keyable =1 )
		cmds.setAttr (main_A + ".offset",e =1,keyable =1 )
		cmds.setAttr (main_A + ".envelope",e =1,keyable =1 )
		cmds.setAttr (main_A + ".amplitude",e =1,keyable =1 )
		cmds.setAttr (main_A + ".wavelength",e =1,keyable =1 )
		cmds.setAttr (main_A + ".dropoff",e =1,keyable =1 )
		cmds.setAttr (main_A + ".dropoffPosition",e =1,keyable =1 )
		cmds.setAttr (main_A + ".minRadius",e =1,keyable =1 )
		cmds.setAttr (main_A + ".maxRadius",e =1,keyable =1 )

		cmds.connectAttr(main_A+".offset",sine + ".offset",f=1 )
		cmds.connectAttr(main_A+".envelope",sine + ".envelope",f=1 )
		cmds.connectAttr(main_A+".amplitude",sine + ".amplitude",f=1 )
		cmds.connectAttr(main_A+".wavelength",sine + ".wavelength",f=1 )
		cmds.connectAttr(main_A+".dropoff",sine + ".dropoff",f=1 )
		cmds.connectAttr(main_A+".dropoffPosition",sine + ".dropoffPosition",f=1 )
		cmds.connectAttr(main_A+".minRadius",sine + ".minRadius",f=1 )
		cmds.connectAttr(main_A+".maxRadius",sine + ".maxRadius",f=1 )	

		cmds.setAttr(Handle + ".hiddenInOutliner",1)
		cmds.setAttr(Handle + ".visibility" ,0)
		cmds.parentConstraint (main_A,Handle,mo=1,weight =1)
		cmds.scaleConstraint (main_A,Handle,mo=1,weight =1)	
	elif int ==2 :
		cmds.addAttr(main_A,ln ="wave",at ="enum",en = "--------------------------:")
		cmds.addAttr(main_A,ln ="offset",at ="double",min = 0 , max = 100,dv=0)
		cmds.addAttr(main_A,ln ="lengthScale",at ="double",min = 0 ,dv=100)
		cmds.addAttr(main_A,ln ="maxScale",at ="double",min =0,dv=1)
		cmds.addAttr(main_A,ln ="height",at ="double",min = 3,dv=3)
		cmds.addAttr(main_A,ln ="subdivisionsHeight",at ="double",min = 10 ,dv=10)
		
		cmds.setAttr (main_A + ".wave",e =1,keyable =1 )
		cmds.setAttr (main_A + ".offset",e =1,keyable =1 )
		cmds.setAttr (main_A + ".lengthScale",e =1,keyable =1 )
		cmds.setAttr (main_A + ".maxScale",e =1,keyable =1 )
		cmds.setAttr (main_A + ".height",e =1,keyable =1 )
		cmds.setAttr (main_A + ".subdivisionsHeight",e =1,keyable =1 )


		ShadingNode =cmds.shadingNode ("multiplyDivide",asUtility= 1,name = polyP_Transform+"multiplyDivide")
		cmds.connectAttr(main_A+".offset", ShadingNode+".input1X",f=1)
		cmds.connectAttr(ShadingNode+".outputX", Deformer[0]+".offset",f=1)
		cmds.setAttr (ShadingNode+".input2X",0.01)

		#lengthScale_ShadingNode =cmds.shadingNode ("multiplyDivide",asUtility= 1,name = polyP_Transform+"lengthScaleMultiplyDivide")
		cmds.connectAttr(main_A+".lengthScale", ShadingNode+".input1Y",f=1)
		#cmds.connectAttr(ShadingNode+".outputX", Deformer[0]+".offset",f=1)
		cmds.connectAttr(ShadingNode+".outputY", Deformer[0]+".lengthScale",f=1)
		cmds.setAttr (ShadingNode+".input2Y",0.01)

		cmds.connectAttr(main_A+".maxScale", Deformer[0]+".maxScale",f=1)
		cmds.connectAttr(main_A+".height", polyP[1]+".height",f=1)
		cmds.connectAttr(main_A+".subdivisionsHeight",  polyP[1]+".subdivisionsHeight",f=1)

	elif int ==3 :
		cmds.addAttr ( main_A , ln ="wave",at ="enum",en = "--------------------------:")
		cmds.addAttr ( main_A , ln ="offset",at ="double",dv=0)
		cmds.addAttr ( main_A , ln ="amplitude",at ="double",min = -5 , max =5,dv=0.2)
		cmds.addAttr ( main_A , ln ="wavelength",at ="double",min = 0.1 , max = 10,dv=1.3)
		cmds.addAttr ( main_A , ln ="dropoff",at ="double",min = 0 , max = 100,dv=-1)
		cmds.addAttr ( main_A , ln ="lowBound",at ="double",min = -10 , max = 0,dv=0)
		cmds.addAttr ( main_A , ln ="highBound",at ="double",min = 0 , max = 10,dv=1)

		cmds.setAttr ( main_A + ".wave",e =1,keyable =1 )
		cmds.setAttr ( main_A + ".offset",e =1,keyable =1 )
		cmds.setAttr ( main_A + ".amplitude",e =1,keyable =1 )
		cmds.setAttr ( main_A + ".wavelength",e =1,keyable =1 )
		cmds.setAttr ( main_A + ".dropoff",e =1,keyable =1 )
		cmds.setAttr ( main_A + ".lowBound",e =1,keyable =1 )
		cmds.setAttr ( main_A + ".highBound",e =1,keyable =1 )

		cmds.connectAttr ( main_A + ".offset", sine+".offset",f=1 )
		cmds.connectAttr ( main_A + ".amplitude", sine+".amplitude",f=1 )
		cmds.connectAttr ( main_A + ".wavelength ",sine+".wavelength",f=1 )
		cmds.connectAttr ( main_A + ".dropoff ",sine+".dropoff",f=1 )
		cmds.connectAttr ( main_A + ".highBound", sine+".highBound",f=1 )
		cmds.connectAttr ( main_A + ".lowBound", sine+".lowBound",f=1 )
	
	Attrs = ["tx","ty","tz","rx","ry","rz","sx","sy","sz","v"]
	for Attr in  Attrs:
		cmds.setAttr (polyP_Transform +"."+ Attr ,lock= 1 )
		cmds.setAttr (polyP_Transform +"."+ Attr ,k =0)

	mel.eval('evalDeferred( "showEditor sequence_file")')
	cmds.select(main_A,r=1)
	cmds.evalDeferred("mel.eval('ShowAttributeEditorOrChannelBox;')")